﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OneStop.UI
{
    public class NumericFormatConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Format(value, parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Format(value, parameter);
        }

        object Format(object value, object param)
        {
            if (value == null)
                return value;

            param = "{0:" + param + "}";

            int ri;
            double rd;

            if (int.TryParse(value.ToString(), out ri))
            {
                return string.Format(param.ToString(), ri);
            }
            else if (double.TryParse(value.ToString(), out rd))
            {
                return string.Format(param.ToString(), rd);
            }
            else
            {
                return string.Format(param.ToString(), value);
            } 
        }

        #endregion
    }

}
